package com.zzw.lock;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.management.*;
import java.util.ArrayList;
import java.util.List;

/**
 * created by zzw
 */
public class MXBeanUtil {

    private static Logger logger = LoggerFactory.getLogger(MXBeanUtil.class);

    public static List<ThreadInfo> getDeadLock(){
        List<ThreadInfo> list = new ArrayList<>();
        ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
        threadMXBean.findDeadlockedThreads();
        long[] threadsIds = threadMXBean.findDeadlockedThreads();
        for(long id:threadsIds){
            ThreadInfo threadInfo = threadMXBean.getThreadInfo(id);
            list.add(threadInfo);
        }
        return list;
    }

    public static void getCodeCacheUsage() throws InterruptedException {
        Thread.sleep(2000);
        ManagementFactory.getPlatformMXBeans(MemoryPoolMXBean.class)
                .stream()
                .filter(e -> MemoryType.NON_HEAP == e.getType())
                .filter(e -> e.getName().startsWith("CodeHeap"))
                .forEach(e -> {
                    System.out.println(e.getName() + "---" + e.getUsage());
                    logger.info("name:{},info:{}",e.getName(),e.getUsage());
                });
    }

    public static void main(String[] args) throws InterruptedException {
        getCodeCacheUsage();
    }
}
